package com.cxq.boot.controller;

import com.cxq.boot.cache.HotTagCache;
import com.cxq.boot.dto.PaginationDTO;
import com.cxq.boot.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private HotTagCache hotTagCache;

    @GetMapping("/")
    public String index(Model model,
                        @RequestParam(value = "page",defaultValue = "1")Integer page,
                        @RequestParam(value = "size",defaultValue = "10")Integer size,
                        @RequestParam(value = "search",required = false)String search,
                        @RequestParam(value = "tag",required = false)String tag) {
        PaginationDTO pagination = questionService.list(page,size,search,tag);//获取所有问题
        List<String> hotsTag = hotTagCache.getHots();
        model.addAttribute("pagination", pagination);
        model.addAttribute("search",search);
        model.addAttribute("tags",hotsTag);
        model.addAttribute("tag",tag);//返回的tag，用户点击分页回显
        return "index";
    }
}
